package dp;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class FlatCutGUI extends JFrame {
    private static final long serialVersionUID = 2164362424367L;
    private JTable table;
    private DefaultTableModel model;

    protected FlatCutGUI() {
	super();
    }

    public void run() {
	setLocationByPlatform(true);
	setMinimumSize(new Dimension(250, 160));
	initialize();
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	pack();
	setVisible(true);
    }

    private void initialize() {
	JPanel panel = new JPanel(new GridBagLayout());
	panel.setOpaque(true);
	int i = 1;

	JLabel wightLabel = new JLabel("Высота:");
	GridBagConstraints c = new GridBagConstraints();
	c.anchor = GridBagConstraints.FIRST_LINE_START;
	c.fill = GridBagConstraints.BOTH;
	c.weightx = 1;
	c.gridy = i;
	c.insets = new Insets(10, 10, 10, 10);
	panel.add(wightLabel, c);

	final JTextField wightText = new JTextField("4");
	c = new GridBagConstraints();
	c.anchor = GridBagConstraints.FIRST_LINE_END;
	c.fill = GridBagConstraints.BOTH;
	c.weightx = 3;
	c.gridy = i;
	c.insets = new Insets(10, 10, 10, 10);
	panel.add(wightText, c);
	++i;

	JLabel heightLabel = new JLabel("Ширина:");
	c = new GridBagConstraints();
	c.anchor = GridBagConstraints.FIRST_LINE_START;
	c.fill = GridBagConstraints.BOTH;
	c.weightx = 1;
	c.gridy = i;
	c.insets = new Insets(10, 10, 10, 10);
	panel.add(heightLabel, c);

	final JTextField heightText = new JTextField("3");
	c = new GridBagConstraints();
	c.anchor = GridBagConstraints.FIRST_LINE_END;
	c.fill = GridBagConstraints.BOTH;
	c.weightx = 3;
	c.gridy = i;
	c.insets = new Insets(10, 10, 10, 10);
	panel.add(heightText, c);
	++i;

	model = new DefaultTableModel();
	String[] columnNames = { "Ширина", "Высота", "Цена" };
	for (String string : columnNames) {
	    model.addColumn(string);
	}
	Object[][] dataTable = { { 1, 3, 4 }, { 1, 1, 1 } };
	for (Object[] objects : dataTable) {
	    model.addRow(objects);
	}
	table = new JTable(model);
	table.setFillsViewportHeight(true);
	table.setPreferredScrollableViewportSize(new Dimension(500, 250));
	c = new GridBagConstraints();
	c.anchor = GridBagConstraints.SOUTHWEST;
	c.fill = GridBagConstraints.BOTH;
	c.gridx = 0;
	c.gridy = i;
	c.gridwidth = 2;
	c.insets = new Insets(10, 10, 10, 10);
	panel.add(new JScrollPane(table), c);
	++i;

	JButton addNew = new JButton("Добавить строчку");
	addNew.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent e) {
		((DefaultTableModel) table.getModel()).addRow(new Object[3]);
	    }
	});
	addNew.setSize(50, 100);
	c = new GridBagConstraints();
	c.anchor = GridBagConstraints.WEST;
	c.gridx = 0;
	c.gridy = i;
	c.weightx = 1;
	c.insets = new Insets(10, 10, 10, 10);
	panel.add(addNew, c);

	JButton deleteSelected = new JButton("Удалить выделенную");
	deleteSelected.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent e) {
		model.removeRow(table.getSelectedRow());
	    }
	});
	deleteSelected.setSize(50, 100);
	c = new GridBagConstraints();
	c.anchor = GridBagConstraints.WEST;
	c.gridx = 1;
	c.gridy = i;
	c.weightx = 1;
	c.insets = new Insets(10, 10, 10, 10);
	panel.add(deleteSelected, c);
	++i;

	JButton evaluate = new JButton("Посчитать");
	final JTextArea resultText = new JTextArea();
	evaluate.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent e) {
		Vector<Vector<Integer>> rows = model.getDataVector();
		List<Integer> h = new ArrayList<Integer>();
		List<Integer> w = new ArrayList<Integer>();
		List<Integer> v = new ArrayList<Integer>();
		for (Vector<Integer> row : rows) {
		    w.add(row.get(0));
		    h.add(row.get(1));
		    v.add(row.get(2));
		}
		FlatCut flatCut2 = new FlatCut(Integer.parseInt(wightText
			.getText()), Integer.parseInt(heightText.getText()), w,
			h, v);
		flatCut2.evaluate();
		resultText.setText(flatCut2.toString());
		pack();
	    }
	});
	evaluate.setSize(50, 100);
	c = new GridBagConstraints();
	c.anchor = GridBagConstraints.WEST;
	c.gridx = 0;
	c.gridy = i;
	c.weightx = 1;
	c.insets = new Insets(10, 10, 10, 10);
	panel.add(evaluate, c);

	resultText.setSize(100, 100);
	c = new GridBagConstraints();
	c.fill = GridBagConstraints.BOTH;
	c.gridx = 1;
	c.gridy = i;
	c.weightx = 1;
	c.gridheight = 2;
	c.insets = new Insets(10, 10, 10, 10);
	panel.add(resultText, c);
	++i;

	getContentPane().add(new JScrollPane(panel));
    }

    public static void main(String[] args) {
	new FlatCutGUI().run();
    }
}
